home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Games / Arashi 1.1.1 / source code / For your think c folder / VA Kit ƒ / VAStarfield.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-09  |  3.5 KB  |  188 lines  |  [TEXT/KAHL]

  1. /*/
  2.      Project Arashi: VAStarfield.c
  3.      Major release: Version 1.1d2, 9/5/95
  4.  
  5.      Last modification: Wednesday, September 9, 1992, 21:42
  6.      Created: Wednesday, February 6, 1991, 4:53
  7.  
  8.      Copyright © 1991-1992, Juri Munkki
  9. /*/
  10.  
  11. #include "VA.h"
  12. #include "STORM.h"
  13.  
  14. #define    NEWNUMBER    2
  15. #define    MAXSTARS    75L
  16. #define    STARSPEED    5
  17.  
  18. typedef struct
  19. {    
  20.     int        x,y,z;
  21. } MegaStar;
  22.  
  23. MegaStar    *MegaStars;
  24. int            StarsUsed;
  25. Point        StarTwist;
  26. Point        NewStarTwist;
  27. Point        OldStarTwist;
  28. int            Twister;
  29.  
  30. void    FeedStars()
  31. {
  32.     register    int            i;
  33.     register    MegaStar    *starp;
  34.     register    Point        area,doublearea;
  35.  
  36.     if(StarsUsed<MAXSTARS)
  37.     {
  38.         area.h=VA.frame.right>>1;
  39.         area.v=VA.frame.bottom>>1;
  40.     
  41.         doublearea.h=VA.frame.right;
  42.         doublearea.v=VA.frame.bottom;
  43.     
  44.         starp = MegaStars+StarsUsed;
  45.         if(MAXSTARS-StarsUsed < NEWNUMBER)
  46.         {    i=MAXSTARS-StarsUsed;
  47.         }
  48.         else
  49.         {    i=NEWNUMBER;
  50.         }
  51.         StarsUsed += i;
  52.         
  53.         while(i--)
  54.         {    starp->x = ((unsigned int)FastRandom()) % doublearea.h - area.h + StarTwist.h;
  55.             starp->y = ((unsigned int)FastRandom()) % doublearea.v - area.v + StarTwist.v;
  56.             starp->z = DEPTH+STARTDEPTH;
  57.             starp++;
  58.         }
  59.     }
  60. }
  61.  
  62. int        TwistTweeker[]={ 1,1,1,2,2,2,3,3,4,5,6,7,8,10,12,16,
  63.                          20,22,24,25,26,27,28,29,29,30,30,30,31,31,31,32};
  64.  
  65. void    RideStars()
  66. {
  67.     register    int            i;
  68.     register    MegaStar    *starp;
  69.     register    int            x,y;
  70.                 Point        midpoint;
  71.                 Point        area,doublearea;
  72.     
  73.     if(StarTwist.h != NewStarTwist.h || StarTwist.v != NewStarTwist.v)
  74.     {    int        TwistFactor;
  75.         Twister++;
  76.         TwistFactor=TwistTweeker[Twister];
  77.  
  78.         StarTwist.h = OldStarTwist.h + (((NewStarTwist.h - OldStarTwist.h) * Twister)>>5);
  79.         StarTwist.v = OldStarTwist.v + (((NewStarTwist.v - OldStarTwist.v) * Twister)>>5);
  80.     }
  81.     else
  82.     {    OldStarTwist = StarTwist;
  83.         Twister = 0;
  84.     }
  85.     
  86.     midpoint.h=(VA.frame.right>>1)+StarTwist.h;
  87.     midpoint.v=(VA.frame.bottom>>1)+StarTwist.v;
  88.  
  89.     VA.color=0;
  90.     starp=MegaStars;
  91.     
  92.     if(VA.Late)
  93.     {    for(i=0;i<StarsUsed;i++)
  94.         {    if(starp->z<=STARSPEED)
  95.             {    *starp=MegaStars[--StarsUsed];
  96.                 starp--;
  97.                 i--;
  98.             }
  99.             else
  100.             {    starp->z -= STARSPEED;
  101.             }
  102.             starp++;
  103.         }
  104.     }
  105.     else
  106.     {    for(i=0;i<StarsUsed;i++)
  107.         {    x= (((starp->x-StarTwist.h) * STARTDEPTH) / starp->z) + midpoint.h;
  108.             y= (((starp->y-StarTwist.v) * STARTDEPTH) / starp->z) + midpoint.v;
  109.             
  110.             if(x<=0 || x>=VA.frame.right || y<=0 || y>=VA.frame.bottom || starp->z<=STARSPEED)
  111.             {    *starp=MegaStars[--StarsUsed];
  112.                 starp--;
  113.                 i--;
  114.             }
  115.             else
  116.             {    
  117.                 if(starp->z<STARTDEPTH*2)
  118.                         VA.color=0;
  119.                 else    VA.color=(starp->z-STARTDEPTH)*7/(DEPTH-1);
  120.  
  121.                 VASafeSpot(x,y);
  122.                 starp->z -= STARSPEED;
  123.             }
  124.             starp++;
  125.         }
  126.     }
  127. }
  128.  
  129. void    CloseStars()
  130. {
  131.     StarsUsed=0;
  132.     DisposPtr(MegaStars);
  133.     MegaStars=0;
  134. }
  135. void    OpenStars(Colors)
  136. Handle    Colors;
  137. {
  138.     register    ColorSpec    *colp;
  139.     register    int            i;
  140.  
  141.     HandToHand(&Colors);
  142.  
  143.     StarTwist.h=0;
  144.     StarTwist.v=0;
  145.     colp = (ColorSpec *)*Colors;
  146.     HLock(Colors);
  147.     for(i=0;i<7;i++)
  148.     {    colp[i].rgb.red        =colp[8].rgb.red   * (7L-i) / 7;
  149.         colp[i].rgb.green    =colp[8].rgb.green * (7L-i) / 7;
  150.         colp[i].rgb.blue    =colp[8].rgb.blue  * (7L-i) / 7;
  151.     }
  152.     HUnlock(Colors);
  153.     VASetColors(Colors);
  154.     DisposHandle(Colors);
  155.     
  156.     StarsUsed=0;
  157.     MegaStars=(MegaStar *)NewPtr(MAXSTARS * sizeof(MegaStar));
  158. }
  159.  
  160. #ifdef TEST_STARS
  161. void    TestStars()
  162. {
  163.     Point    Mouser;
  164.  
  165.     OpenStars(GetResource('CLOT',1001));
  166.     VAEraseBuffer();
  167.     do
  168.     {    FeedStars();
  169.         RideStars();
  170.         VAStep();
  171.         GetMouse(&Mouser);
  172.         VALine(0,0,Mouser.h,Mouser.v);
  173.         if(Twister==0)
  174.         {
  175.             NewStarTwist.h=(Mouser.h-VA.frame.right/2);
  176.             NewStarTwist.v=(Mouser.v-VA.frame.bottom/2);
  177.         }    
  178.         
  179.     }    while(!Button());
  180.  
  181.     while(StarsUsed)
  182.     {    RideStars();
  183.         VAStep();
  184.     }
  185.     VASetColors(GetResource('CLOT',1000));
  186.     CloseStars();
  187. }
  188. #endif